package com.frank.subject.infra.es.subject.convert;

import com.frank.config.es.config.utils.ListUtils;
import com.frank.config.es.config.utils.ObjectToMapUtil;
import com.frank.subject.infra.es.subject.entity.SubjectInfoEs;
import com.frank.subject.infra.es.subject.fields.SubjectInfoFields;
import lombok.SneakyThrows;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/7/7
 * @description
 */
public class SubjectInfoEsConvert {


    @SneakyThrows
    public static SubjectInfoEs convertSearchResponseToSubjectInfoEs(SearchHit hit) {
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        SubjectInfoEs result = convertSourceMapToSubjectInfoEs(sourceAsMap);
        if (Objects.isNull(result)) {
            return null;
        }
        // 设置高亮展示
        Map<String, HighlightField> highlightFields = hit.getHighlightFields();
        HighlightField highlightField = highlightFields.get(SubjectInfoFields.SUBJECT_NAME);
        if (Objects.nonNull(highlightField)) {
            Text[] fragments = highlightField.getFragments();
            StringBuilder stringBuilder = new StringBuilder();
            for (Text fragment : fragments) {
                stringBuilder.append(fragment);
            }
            result.setSubjectName(stringBuilder.toString());
        }
        return result;
    }


    @SuppressWarnings("all")
    public static Map<String, Object> convertSubjectInfoEsToMap(SubjectInfoEs subjectInfoEs) {
        return ObjectToMapUtil.convertToMap(subjectInfoEs);
    }

    @SneakyThrows
    public static SubjectInfoEs convertSourceMapToSubjectInfoEs(Map<String, Object> source) {
        if (CollectionUtils.isEmpty(source)) {
            return null;
        }
        SubjectInfoEs result = new SubjectInfoEs();
        result.setId(MapUtils.getLong(source, SubjectInfoFields.ID));
        result.setSubjectName(MapUtils.getString(source, SubjectInfoFields.SUBJECT_NAME));
        result.setSubjectType(MapUtils.getInteger(source, SubjectInfoFields.SUBJECT_TYPE));
        result.setSubjectAnswer(MapUtils.getString(source, SubjectInfoFields.SUBJECT_ANSWER));
        result.setCategoryId(MapUtils.getLong(source, SubjectInfoFields.CATEGORY_ID_LIST));
        result.setLabelId(MapUtils.getLong(source, SubjectInfoFields.LABEL_ID_LIST));
        result.setSubjectScore(MapUtils.getInteger(source, SubjectInfoFields.SUBJECT_SCORE));
        result.setCreatedBy(MapUtils.getString(source, SubjectInfoFields.CREATED_BY));
        result.setLabelIdList(ListUtils.convertObjectToList(source.get(SubjectInfoFields.LABEL_ID_LIST)));
        result.setCategoryIdList(ListUtils.convertObjectToList(source.get(SubjectInfoFields.CATEGORY_ID_LIST)));
        String dateStr = MapUtils.getString(source, SubjectInfoFields.CREATED_TIME);
        if (StringUtils.isNotBlank(dateStr)) {
            result.setCreatedTime(DateUtils.parseDate(dateStr, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
        }
        return result;
    }





}
